﻿using System;
using System.Drawing;
using System.Windows.Forms;
using System.Text.RegularExpressions;

//  Nati Nissim ID: 038589602 
//  Michael Biniashvili ID: 061669966


namespace SimulationHW2
{
    public partial class MenuSimulation : Form
    {
        public MenuSimulation()
        {
            InitializeComponent();
            showTablesToolStripMenuItem.Enabled = false;
        }

        #region Event
        /// <summary>
        /// Read Text File
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnReadTextFile_Click(object sender, EventArgs e)
        {
            using (OpenFileDialog openFileDialog = new OpenFileDialog())
            {
                openFileDialog.Title = "Open Text File";
                openFileDialog.Filter = "TEXT Files|*.txt";
                if (openFileDialog.ShowDialog() == DialogResult.OK)
                {
                    showTablesToolStripMenuItem.Enabled = true;
                    toolStripStatusLabelFilePath.Text = openFileDialog.FileName;
                    ReadAndSplit temp = new ReadAndSplit(openFileDialog.FileName);
                    if(temp.readFile()) InitializeForm();
                }
            }
        }
       
        /// <summary>
        /// Close Windows form
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnExit_Click(object sender, EventArgs e)
        {
            this.Close();
        }
        /// <summary>
        /// Btn check Fisher
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnCheckF_Click(object sender, EventArgs e)
        {
            if (txtFisherTable.Text != string.Empty)
            {
                fTable = Convert.ToDouble(txtFisherTable.Text);

                if (fCalculated < fTable)
                {
                    textBox1.Enabled = lblResult.Enabled = lblCheckFisher.Enabled = true;
                    lblResult.Text = string.Format("Regression Equation Y^ = {0} and the function:", regression);
                    textBox1.Lines = strResult;
                    lblCheckFisher.BackColor = Color.GreenYellow;
                    lblCheckFisher.Text = "Test Fisher Successed";
                }
                else
                {
                    textBox1.Text = lblResult.Text = string.Empty;
                    textBox1.Enabled = lblResult.Enabled = false;
                    lblCheckFisher.BackColor = Color.Red;
                    lblCheckFisher.Text = "Test Fisher Unsuccessfully";
                }
            }
        }
        /// <summary>
        /// Btn Show Table Simulation
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnOutputTables_Click(object sender, EventArgs e)
        {
            // Show Simulation Tables
            ShowTableSimulation showTableAndSimulation = new ShowTableSimulation();
            showTableAndSimulation.ShowDialog();
        }

        /// <summary>
        /// Check text box regex
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="TextBoxtype"></param>
        /// <returns></returns>
        private Boolean checkregLetter(object sender, string TextBoxtype)
        {
            Regex regex = new Regex("^[0-9]*.[0-9]*$");
            if (regex.IsMatch(TextBoxtype))
            {
                return true;
            }
            else if (TextBoxtype.Equals(""))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Check text box regex
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="TextBoxtype"></param>
        /// <returns></returns>
        private Boolean checkregLetterl(object sender, string TextBoxtype)
        {
            Regex regex = new Regex("^[0-9]*[a-zA-Z]+$");
            if (regex.IsMatch(TextBoxtype))
            {
                return true;
            }
            else if (TextBoxtype.Equals(""))
            {
                return true;
            }
            else
            {
                return false;
            }
        }

        /// <summary>
        /// Text Changed text box Fisher table test
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void txtFisherTable_TextChanged(object sender, EventArgs e)
        {
            if (!checkregLetter(sender, txtFisherTable.Text))
            {
                txtFisherTable.Text = txtFisherTable.Text.Substring(0, (txtFisherTable.Text.Length - 1));
            }
            else if (checkregLetterl(sender, txtFisherTable.Text))
            {
                txtFisherTable.Text = "";
            }
        }

        private void inputFileToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnReadTextFile_Click(null, null);
        }

        private void showTablesToolStripMenuItem_Click(object sender, EventArgs e)
        {
            btnOutputTables_Click(null,null);
        }

        private void exitToolStripMenuItem_Click(object sender, EventArgs e)
        {
            if (MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
            {
                Application.Exit();
            }
        }

        private void aboutToolStripMenuItem_Click(object sender, EventArgs e)
        {
            AboutSimulation about = new AboutSimulation();
            about.Show();
        }

        private void MenuSimulation_KeyPress(object sender, KeyPressEventArgs e)
        {
            if (e.KeyChar == (char)27)
            {
                if (MessageBox.Show("Really Quit?", "Exit", MessageBoxButtons.OKCancel) == DialogResult.OK)
                {
                    Application.Exit();
                } 
            }
        }
         
        #endregion Event

        #region Initial functions
        /// <summary>
        /// Initial Form textbox from text file
        /// </summary>
        private void InitializeForm()
        {
            if (PropertiesSimulation.IsPartialType) txtType.Text = "partial";
            else txtType.Text = "full";
            txtDegree.Text = PropertiesSimulation.Degree.ToString();
            txtLevel.Text = PropertiesSimulation.Level.ToString();
            txtCombinationId.Text = PropertiesSimulation.CombinationId.ToString();
            txtAmount.Text = PropertiesSimulation.Amount.ToString();
            txtT_Coefficient.Text = PropertiesSimulation.Coefficient.ToString();
            if (PropertiesSimulation.Degree != 0 && PropertiesSimulation.Level != 0 && PropertiesSimulation.CombinationId != 0 && PropertiesSimulation.Amount != 0 && PropertiesSimulation.Coefficient != 0)
            {
                createTables();
            }
            else
            {
                MessageBox.Show("The text file invalid.", "Error Read File", MessageBoxButtons.OK,
                                                MessageBoxIcon.Error,
                                                MessageBoxDefaultButton.Button1);
            }
        }

        /// <summary>
        /// Create Tables and clear all tables
        /// </summary>
        private void createTables()
        {
            PropertiesSimulation.dataTableZ.Columns.Clear();
            PropertiesSimulation.dataTableZ.Clear();
            PropertiesSimulation.dataTableZ.Dispose();

            PropertiesSimulation.dataTable.Columns.Clear();
            PropertiesSimulation.dataTable.Clear();
            PropertiesSimulation.dataTable.Dispose();

            PropertiesSimulation.dataTableB.Columns.Clear();
            PropertiesSimulation.dataTableB.Clear();
            PropertiesSimulation.dataTableB.Dispose();

            PropertiesSimulation.dataTableT.Columns.Clear();
            PropertiesSimulation.dataTableT.Clear();
            PropertiesSimulation.dataTableT.Dispose();

            StudentTable studentTable = new StudentTable();
            cakcuDataTables = new CalculateTables();

            fCalculated = cakcuDataTables.fCalculated;
            k1 = cakcuDataTables.k1;
            k2 = cakcuDataTables.k2;
            strResult = cakcuDataTables.strResult;
            regression = cakcuDataTables.sumResult;

            txtFisherTable.Enabled = true;
            btnCheckF.Enabled = true;
            btnOutputTables.Enabled = true;
            lblOutPut.Text = string.Format("Fcalculate: {0}\nFtable (K1: {1},K2: {2}) ", fCalculated, k1, k2);
        }
        #endregion Initial functions

    }
}
